//
//  PhotoBrowserViewController.swift
//  CLWeiBo
//
//  Created by 程雷 on 15/7/3.
//  Copyright © 2015年 程雷. All rights reserved.
//

import UIKit
import FFAutoLayout
import SVProgressHUD

class PhotoBrowserViewController: UIViewController {


    func close ()
    {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func save()
    {
        // 1.拿到图像
        let image = currentImageView().image!
        // 2.保存图像 －回调方法的参数格式是固定的
        UIImageWriteToSavedPhotosAlbum(image,self,"image:didFinishSavingWithError:contextInfo:", nil)
        
    }
    ///  获得当前图片的索引
    func currentImageIndex() -> Int {
        return collectionView.indexPathsForVisibleItems().last!.item
    }
    
    ///  获取当前显示的图像视图
    func currentImageView() ->UIImageView{
        // 0 拿到当前 cell
        let indexPath = collectionView.indexPathsForVisibleItems().last
        let cell = collectionView.cellForItemAtIndexPath(indexPath!) as! PhotoViewerCell
        // 1. 拿到图像
        return cell.imageView
    }
    
    func image(image :UIImage, didFinishSavingWithError error:NSError?, contextInfo :AnyObject) {
        
        if error != nil {
            SVProgressHUD.showErrorWithStatus("保存出错")
        }
        else
        {
            SVProgressHUD.showSuccessWithStatus("保存成功")
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        print(imageURLs)

    }
    // 将要出现视图  视图ok collectionView  不 OK 大小 为 （0，0，0，0）
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("\(__FUNCTION__)\(view.frame)\(collectionView.frame)")
    }
    //将要布局 视图ok collectionView  不 OK 大小 为 （0，0，0，0）
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print("\(__FUNCTION__)\(view.frame)\(collectionView.frame)")
    }
    // 已经出现布局 视图ok collectionView OK

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print("\(__FUNCTION__)\(view.frame)\(collectionView.frame)")
        
        prepareLayout()
        // 跳转到指定页面
        let indexPath = NSIndexPath(forItem: currentIndex, inSection: 0)
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: false)
    }
    
    private func prepareLayout()
    {
        // 设定所有 item 的 尺寸
        layout.itemSize = collectionView.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView.pagingEnabled = true
    }
    
    override func loadView() {
        
        // 将视图大小设大
        var screenBounds = UIScreen.mainScreen().bounds
        screenBounds.size.width += 20
        
        view = UIView(frame:screenBounds)
        
        view.backgroundColor = UIColor.blackColor()
        view.addSubview(collectionView)
        view.addSubview(saveButton)
        view.addSubview(closeButton)
        
        collectionView.ff_Fill(view)
        saveButton.ff_AlignInner(ff_AlignType.BottomRight, referView: view,size:CGSize(width: 60, height: 30), offset: CGPoint(x: -12, y: -12))
        
        closeButton.ff_AlignInner(ff_AlignType.BottomLeft, referView: view, size: CGSize(width: 60, height: 30), offset: CGPoint(x: 12, y: -12))
        
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        prepareCollectionView()

    }
    private func prepareCollectionView()
    {
        collectionView.backgroundColor = UIColor.clearColor()
        collectionView.dataSource = self
        collectionView.delegate = self

        collectionView.registerClass(PhotoViewerCell.self, forCellWithReuseIdentifier: CLPhotoBrowserCellReuseIdentifier)
    }
    init(urls:[NSURL],index:Int)
    {
        //初始化本类的属性
        imageURLs = urls
        currentIndex = index
        //再调用系统提供的 父类 构造函数
        super.init(nibName:nil,bundle:nil)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 图像数组
    private var imageURLs:[NSURL]
    //用户选中图片的索引
    private var currentIndex: Int
    
    // MARK ---懒加载
    lazy private var layout = UICollectionViewFlowLayout()
    lazy private var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.layout)
    
    lazy private var saveButton: UIButton = UIButton(title:"保存",fontSize:14)
    lazy private var closeButton: UIButton = UIButton(title:"关闭",fontSize:14)
    // 照片的缩放比例与 oc 类似，分类中同样不能包含存储性属性
    private var photoScale:CGFloat = 0

}

private let  CLPhotoBrowserCellReuseIdentifier = "CLPhotoBrowserCellReuseIdentifier"

extension PhotoBrowserViewController :UICollectionViewDataSource,UICollectionViewDelegate,PhotoViewCellDelegate{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CLPhotoBrowserCellReuseIdentifier, forIndexPath: indexPath) as! PhotoViewerCell
        cell.backgroundColor = UIColor.clearColor()
        //调用此方法说明上面的数据行有效，证明数组一定有内容
        cell.imageURL = imageURLs[indexPath.item]
        
        cell.photoDelegate = self
        
        return cell
    }
    // 全屏状态下，此方法不会被触发
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print(__FUNCTION__)
    }
    
    func photoViewCellDidTapImage() {
        // 关闭界面
        close()
    }
    ///  缩放进行中
    func photoViewDidZooming(scale: CGFloat) {
        print(scale)
        //交互式转场
        
        photoScale = scale
        // 隐藏控件 
        hideController(photoScale<1.0)
        // 判断缩放比例小于 1 开始 交互式转场
        if photoScale < 1.0
        {
            startInteractiveTransition(self)
        } else{
            //恢复形变
            view.transform = CGAffineTransformIdentity
            view.alpha = 1
        }
    }
    
    func photoViewerDidEndZoom() {
        //判断当前的缩放比例
        if photoScale < 0.8
        {
            //直接关闭 －－ 告诉转场动画借宿
            completeTransition(true)
        }
    }
   
    /// 隐藏控件
    private func hideController(isHidden:Bool)
    {
        view.backgroundColor = isHidden ? UIColor.clearColor() :UIColor.blackColor()
        saveButton.hidden = isHidden
        closeButton.hidden = isHidden
    }
}

extension PhotoBrowserViewController : UIViewControllerInteractiveTransitioning,UIViewControllerContextTransitioning{
   
    ///  <#Description#>
    func startInteractiveTransition(transitionContext: UIViewControllerContextTransitioning) {
        // 缩放视图
        view.transform = CGAffineTransformMakeScale(photoScale, photoScale)
        // 设置透明度
        view.alpha = photoScale
    }
    
    /// 转场动画中这个函数很重要，告诉转场动画结束
    func completeTransition(didComplete: Bool)
    {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func containerView() -> UIView?
    {
        return self.view.superview
    }
    
    func isAnimated() -> Bool {return true}
    
    func isInteractive() -> Bool { return true}

    
    func transitionWasCancelled() -> Bool {
        return true
    }
    
    func presentationStyle() -> UIModalPresentationStyle{
        return UIModalPresentationStyle.Custom
    }

    func updateInteractiveTransition(percentComplete: CGFloat) {}
    func finishInteractiveTransition() {}
    func cancelInteractiveTransition() {}
    
    func viewControllerForKey(key: String) -> UIViewController?
    {
        return self
    }
    
    func viewForKey(key: String) -> UIView?
    {
        return self.view
    }
    
    @available(iOS 8.0, *)
    func targetTransform() -> CGAffineTransform
    {
        return CGAffineTransformIdentity
    }
    
    func initialFrameForViewController(vc: UIViewController) -> CGRect
    {
        return CGRectZero
    }
    func finalFrameForViewController(vc: UIViewController) -> CGRect
    {
        return CGRectZero
    }
}

